package com.str.queue;
import java.io.*;

public class QueueTest {
	
  private static Queue createQueue(int i) {
  	if (i == 1) return new ArrayQueue();
    if (i == 2) return new LinkedQueue();
  	return null;
  }   

  public static void main(String args[]) throws IOException {
    BufferedReader stdin = new BufferedReader(
                           new InputStreamReader(System.in));
    
    while (true) {
      System.out.println("1. Create Array Queue");
      System.out.println("2. Create Linked Queue");    
      System.out.print("\tEnter your choice(Enter to quit): ");     
      
      String inputLine = stdin.readLine();      
      if (inputLine.equals("")) {
        System.out.println("End of run.");
        System.exit(0);
      }
      
      int choice = Integer.parseInt(inputLine);
      Queue strQueue = createQueue(choice);
      if (strQueue == null) {
        System.out.println("Invalid choice.");
        continue;
      }
    
      while (true) {	
        System.out.println("-------------------------");
        System.out.println("     1. Clear");      
        System.out.println("     2. Enqueue");    
        System.out.println("     3. Dequeue");
        System.out.println("     4. Peek");
        System.out.print("\tEnter your choice(Enter to quit): ");
      
        inputLine = stdin.readLine();      
        if (inputLine.equals("")) break;
        choice = Integer.parseInt(inputLine);
      
        switch (choice) {
          case 1:
            System.out.println("Cleared.");
            strQueue.clear();
            break;
          
          case 2: 
            System.out.print("Enter a string: ");
            Object val = stdin.readLine();
            strQueue.enqueue(val);
            break;
          
          case 3: 
            if (strQueue.isEmpty())
              System.out.println("Queue is empty.");
            else {
              val = strQueue.dequeue();
              System.out.println(val + " has been dequeued.");
            }
            break;
          
          case 4: 
            if (strQueue.isEmpty())
              System.out.println("Queue is empty.");
            else {
              val = strQueue.peek();
              System.out.println("The front value is " + val);
            }
            break;
          
          default: 
            System.out.println("Invalid choice.");
            continue;
        }
      
        System.out.println(strQueue);
        System.out.println("Size = " + strQueue.size());
      }
    }
  }
}
